// ...existing code...
// ...existing code...
package com.example.tank.entity;

import java.awt.*;

public class Player {
    private int x;
    private int y;
    private int width = 40;
    private int height = 40;
    private int speed = 5;
    private int direction = 0; // 0-up, 1-right, 2-down, 3-left
    public int getSpeed() { return speed; }
    public int getDirection() { return direction; }
    public void setDirection(int d) { direction = d; }
    // Removed duplicate getX and getY methods
    public void setX(int x) { this.x = x; }
    public void setY(int y) { this.y = y; }
    // 添加面板边界常量
    private static final int PANEL_WIDTH = 800;
    private static final int PANEL_HEIGHT = 600;
    
    public Player(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    public void moveUp() {
        if (y - speed >= 0) {  // 检查上边界
            y -= speed;
            direction = 0;
        }
    }
    
    public void moveDown() {
        if (y + height + speed <= PANEL_HEIGHT) {  // 检查下边界
            y += speed;
            direction = 2;
        }
    }
    
    public void moveLeft() {
        if (x - speed >= 0) {  // 检查左边界
            x -= speed;
            direction = 3;
        }
    }
    
    public void moveRight() {
        if (x + width + speed <= PANEL_WIDTH) {  // 检查右边界
            x += speed;
            direction = 1;
        }
    }
    
    public void draw(Graphics g) {
        // 绘制坦克主体（更接近经典样式）
        g.setColor(Color.GREEN);
        
        // 主体矩形
        g.fillRect(x + 5, y + 5, width - 10, height - 10);
        
        // 绘制坦克履带
        g.setColor(new Color(0, 100, 0)); // 深绿色履带
        g.fillRect(x, y, 5, height);       // 左履带
        g.fillRect(x + width - 5, y, 5, height); // 右履带
        g.fillRect(x, y, width, 5);        // 上履带
        g.fillRect(x, y + height - 5, width, 5); // 下履带
        
        // 绘制炮塔
        g.setColor(Color.DARK_GRAY);
        g.fillRect(x + width/2 - 8, y + height/2 - 8, 16, 16);
        
        // 绘制炮管（根据方向）
        g.setColor(Color.YELLOW);
        switch (direction) {
            case 0: // up
                g.fillRect(x + width/2 - 2, y - 10, 4, 15);
                break;
            case 1: // right
                g.fillRect(x + width, y + height/2 - 2, 15, 4);
                break;
            case 2: // down
                g.fillRect(x + width/2 - 2, y + height, 4, 15);
                break;
            case 3: // left
                g.fillRect(x - 15, y + height/2 - 2, 15, 4);
                break;
        }
    }
    
    public Rectangle getBounds() {
        return new Rectangle(x, y, width, height);
    }
    
    // ...existing code...
    
    public int getX() {
        return x;
    }
    
    public int getY() {
        return y;
    }
    
    public boolean isHit(Rectangle bullet) {
        return getBounds().intersects(bullet);
    }
}